<!DOCTYPE html>
<html lang="en">
<head>
    <title>畅聊Demo-注册</title>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <link rel="icon" href="img/chat.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="css/font-awesome.css"/>
    <link rel="stylesheet" href="css/login.css" type="text/css" media="all"/>
</head>

<body class="background">
<div class="header-w3l">
    <h1>简单畅聊</h1>
</div>
<div class="main-content-agile" id="app">
    <div class="sub-main-w3">
        <h2>注册</h2>
        <form id="registerForm">
            <div class="icon1">
                <input placeholder="用户名" id="username" v-model="user.username" type="text"/>
            </div>
            <div class="icon2">
                <input placeholder="密码" id="password" v-model="user.password" type="password"/>
            </div>
            <div class="clear"></div>
            <input type="button" id="btnRegister" @click="register" value="注册"/>
            <div class="icon1">
                <span id="err_msg" style="color: red;">{{errMessage}}</span>
            </div>
        </form>
    </div>
</div>
<div class="footer">
    <p>**********有限公司 版权所有Copyright 2006-2023 All Rights Reserved</p>
</div>

<script src="js/vue.js"></script>
<script src="js/axios-0.18.0.js"></script>
<script>
    new Vue({
        el: "#app",
        data() {
            return {
                errMessage: "",
                user: {
                    username: "",
                    password: ""
                }
            }
        },
        methods: {
            register() {
                axios.post("user/register", this.user).then(({data: res}) => {
                    if (res.code === 0) {
                        alert("注册成功，请登录！");
                        location.href = "login.html"; // 注册成功后跳转到登录页面
                    } else {
                        this.errMessage = res.msg;
                    }
                }).catch(err => {
                    this.errMessage = "请求失败，请检查网络或服务器";
                });
            }
        }
    });
</script>
</body>
</html>
